package pt.ipleiria.plp.ui;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Form;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.CommandListener;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemCommandListener;
import de.enough.polish.ui.StringItem;
import de.enough.polish.ui.TextField;
import de.enough.polish.util.Locale;
import pt.ipleiria.plp.BlackSpotterMidlet;
import pt.ipleiria.plp.Lixeiras.LixeiraSent;
import pt.ipleiria.plp.Utils;
import pt.ipleiria.plp.threads.CallbackRetrieveLixeirasNearMe;
import pt.ipleiria.plp.threads.GetLixeirasNearMe;

/**
 *
 * @author Ricardo
 */
public class ProcurarEventosPertodeMimScreen extends Form implements CommandListener,
        ItemCommandListener, CallbackRetrieveLixeirasNearMe{

    private BlackSpotterMidlet midlet;
    private Command cmdProx = new Command(Locale.get("cmd.Continuar"), Command.OK, 1);
    private Command cmdExit = new Command(Locale.get("cmd.Cancelar"), Command.CANCEL, 3);
    //#style itemInput
    private TextField distancia = new TextField(Locale.get("proc.dist"), "", 25, TextField.NUMERIC);
    private StringItem max = new StringItem("", Locale.get("mensagem.distMax"));
    //#style itemInput
    private TextField pos = new TextField(Locale.get("newEv.pos"), "", 25, TextField.ANY);
    private StringItem gpsoff = new StringItem("", Locale.get("mensagem.posHelp"));

    public ProcurarEventosPertodeMimScreen(BlackSpotterMidlet midlet) {
        //#style defaultScreen
        super(Locale.get("title.PertoDeMim"));

        this.midlet = midlet;

        //addPos.addCommand(cmdObt);
        this.append(distancia);
        this.append(max);
        this.setCommandListener(this);


        this.append(pos);
        this.append(gpsoff);


        this.addCommand(cmdProx);
        this.addCommand(cmdExit);

    }

    public void commandAction(Command c, Displayable d) {
        System.out.println("Command: " + c.getLabel());
        if (c == cmdExit) {
            midlet.getDisplay().setCurrent(midlet.getMainMenu());
        } else if (c == cmdProx) {


            String error = null;
            int dist = 0;

            double[] position = getPos();
            if (position == null) {
                error = Locale.get("mensagem.pos");
            } else {
                try {
                    if (distancia.getString() != null) {
                        dist = Integer.parseInt(distancia.getString());
                    } else {
                        error = Locale.get("mensagem.distInc");
                    }
                    if (dist > 2000 || dist < 1) {
                        error = Locale.get("mensagem.distInc");
                    }
                } catch (NumberFormatException ex) {
                    error = Locale.get("mensagem.distInc");
                }
            }
            if (error != null) {
                Alert alert = new Alert(Locale.get("title.Erro"), error + error, null, AlertType.WARNING);
                midlet.getDisplay().setCurrent(alert, this);
            } else {
                GetLixeirasNearMe t = new GetLixeirasNearMe(dist, position[0], position[1], midlet, this);
                t.start();
            }
        }
    }

    public void commandAction(Command c, Item item) {
        System.out.println("Command Item: " + c.getLabel());
    }

    public void getLixeirasSent(LixeiraSent[] lixs, String beingEdited) {
        System.out.println("Recieved: " + lixs.length);
        if (lixs == null && beingEdited != null) {
            Alert alert = new Alert(Locale.get("title.Erro"),
                    Locale.get("mensagem.allBeingEdited"), null, AlertType.ERROR);
            midlet.getDisplay().setCurrent(alert, this);
        } else if (lixs == null) {
            Alert alert = new Alert(Locale.get("title.Erro"),
                    Locale.get("mensagem.nonNear"), null, AlertType.ERROR);
            midlet.getDisplay().setCurrent(alert, this);
        } else if (beingEdited == null) {

            Alert alert = new Alert(Locale.get("title.sucesso"),
                    Locale.get("mensagens.sucesso"), null, AlertType.INFO);
            RecievedEventosScreen evs = new RecievedEventosScreen(Locale.get("title.PertoDeMim"), midlet, lixs);
            midlet.getDisplay().setCurrent(alert, evs);
        } else {
            Alert alert = new Alert(Locale.get("title.sucesso"),
                    Locale.get("mensagem.someBeingEdited") + "\n" + beingEdited, null, AlertType.INFO);
            RecievedEventosScreen evs = new RecievedEventosScreen(Locale.get("title.PertoDeMim"), midlet, lixs);
            midlet.getDisplay().setCurrent(alert, evs);
        }
    }

    /**
     * @return the pos element 0 is lat element 1 is long
     */
    private double[] getPos() {

        double[] position = new double[2];

        String[] strs = Utils.split(pos.getString(), ",");

        if (strs.length > 2) {
            return null;
        }
        try {
            position[0] = Double.parseDouble(strs[0]);
            position[1] = Double.parseDouble(strs[1]);
        } catch (NumberFormatException ex) {
            return null;
        }
        return position;
    }
}
